// 支付相关函数
let selectedPaymentMethod = '1';

function showPaymentModal(data) {
    selectedPaymentMethod = '1';
    // document.getElementById('paymentModal').style.display = 'block';
    data.type = "pay";
    data.token = token;
    $.post(FromUrl,data,function(html) {
        document.getElementById('floatBall').insertAdjacentHTML('afterend', html);
    });

}

// 隐藏支付弹窗
function hidePaymentModal() {
    document.getElementById('paymentModal').remove();
}

// 选择支付方式
function selectPaymentMethod(method) {
    selectedPaymentMethod = method;
    const methods = document.querySelectorAll('.payment-method');
    methods.forEach(m => m.classList.remove('selected'));
    document.querySelector(`.payment-method[onclick*="${method}"]`).classList.add('selected');
    updatePaymentButton();
}

// 更新支付按钮状态
function updatePaymentButton() {
    const button = document.querySelector('.btn-payment');
    button.disabled = !selectedPaymentMethod;
}

// 确认支付
function confirmPayment() {
    if (!selectedPaymentMethod) {
        layer.msg('请选择支付方式', {time: 2000});
        return;
    }
    if(isWeixin()){
        selectedPaymentMethod = '2';
    }
    const amount = document.getElementById('amount').value;
    const goods_name = document.getElementById('goods_name').value;
    const num = document.getElementById('num').value;
    const note = document.getElementById('note').value;
    const sign = document.getElementById('sign').value;
    const userid = document.getElementById('userid').value;
    const data = {
        amount: amount,
        type: selectedPaymentMethod,
        uid: userid,
        goods_name: goods_name,
        num: num,
        note: note,
        gid: gid,
        cid:cid,
        is_aiqi_cid:is_aiqi_cid,
        sign: sign,
        token: token
    };
    // 这里添加支付API调用
    $.post(payUrl, data, function(info) {
        console.log("支付结果:",info);
        hidePaymentModal();
        if (info.code == 1) {
            // 根据支付方式跳转到相应的支付页面
            if (selectedPaymentMethod === '2') {
                if(isWeixin()){
                    console.log("微信支付");
                    if (typeof WeixinJSBridge == "undefined") {
                        if (document.addEventListener) {
                            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                        } else if (document.attachEvent) {
                            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                        }
                    } else {
                        console.log(info.data);
                        onBridgeReady(info.data);
                    }


                }else{
                    if (isiOS()) {
                        window.location.href = info.data.pay_url;
                    } else {
                        window.open(info.data.pay_url)
                    }
                }

            } else if (selectedPaymentMethod === '1') {
                console.log('支付宝支付',cid);
                // 支付宝支付
                // window.location = info.data;
                // 支付宝H5支付
                if(cid == 5){
                    if(isiOS()){
                        console.log("新版支付宝支付");
                        var obj = {
                            "action" : "execAlipay",
                            "orderStr" : info.data,
                        };
                        AmapBridge.send(obj,
                            function(res) {
                                console.log(res); // 返回的数据
                            }
                        )
                    }else{
                        console.log("旧版支付宝支付");
                        AmapApp.bridge.ready(function(){
                            AmapApp.bridge.send({
                                "action" : "execAlipay",
                                "orderStr" : info.data,
                            }, function(res) {
                                console.log("支付返回：",res); // 返回的数据
                            });
                        });
                    }
                }else{
                    console.log('不在⾼德端内...');
                     // 直接插入并提交表单
                     window.location.href = info.data;
                    // document.write(info.data);
                }
            }
        } else {
            layer.msg(info.msg || '支付失败，请重试', {time: 2000});
        }
    });
}

function onBridgeReady(data) {
    WeixinJSBridge.invoke('getBrandWCPayRequest', {
        "appId": data.appId,     //公众号名称，由商户传入
        "timeStamp": data.timeStamp,         //时间戳，自1970年以来的秒数
        "nonceStr": data.nonceStr, //随机串
        "package": data.package,
        "signType": data.signType,         //微信签名方式：
        "paySign": data.paySign //微信签名
        },
        function(res) {
            console.log("微信支付返回：",res);
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                // 使用以上方式判断前端返回,微信团队郑重提示：
                //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠，商户需进一步调用后端查单确认支付结果。
            }
        });
}